//
//  ViewController.m
//  21-NSURLRequestAndResponse
//
//  Created by 李昊林 on 16/8/11.
//  Copyright © 2016年 李昊林. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    //NSURl 负责请求地址
    NSURL *baiduURL = [NSURL URLWithString:@"//www.baidu.com/user/wanghuang/1.html?userName=xxx&pwdxxx"];
    //完整的URL对应的字符串
    NSLog(@"//baidu URL.absoluteString:%@",baiduURL.absoluteString);
    //host：主机地址后对应的路线
    NSLog(@"baiduURL.host: %@",baiduURL.host);
    //path：主机地址后对应的路线
    NSLog(@"baiduURL.path:%@",baiduURL.path);
    //fragment
    NSLog(@"baiduURL.fragment:%@",baiduURL.fragment);
    //query:? 以后参数的值
    NSLog(@"baiduURL:%@",baiduURL.query);
    NSLog(@"%@",baiduURL.resourceSpecifier);
    //不可变请求 和 可变请求
    //1.NSURLrequest
    NSURLRequest *baiDu = [NSURLRequest requestWithURL:baiduURL];
    //不可变的
    NSLog(@"old:%@",baiDu.allHTTPHeaderFields);
    NSMutableURLRequest *baiduMutableRequest = [NSMutableURLRequest requestWithURL:baiduURL];
    //1>时间超时，一个请求持续多久就会失败
    baiduMutableRequest.timeoutInterval = 60;
    //2>HTTPMethod 请求的只是 GET POST PUT DELETE
    baiduMutableRequest.HTTPMethod = @"GET";
    //3>allHTTOHeaderFields 设置网络请求头
    [baiduMutableRequest setValue:@"iOS 10.0 / iphone8.1" forHTTPHeaderField:@"User-Agent"];
    //获取对应的头领域
    [baiduMutableRequest valueForHTTPHeaderField:@"User -Agent"];
    //根据字典特性后填入的值回覆盖
    NSLog(@"%@",baiduMutableRequest.allHTTPHeaderFields);
    //4>HttpBody (一般POST请求时，才会使用)
    //一般情况下，都是字典/字符串转换成Data
    //baiduMutableRequest.HTTPBody = [NSData data];
    //一般情况下，都是字典/字符串转换成Data
    baiduMutableRequest.HTTPBody = [NSData data];
    NSHTTPURLResponse *response = [[NSHTTPURLResponse alloc] initWithURL:baiduURL statusCode:200 HTTPVersion:@"HTTP/1.1" headerFields:@{@"data":[NSData data]}];
    // 1> statusCode 状态码
    // response.statusCode
    // 2> allHeaderFields 响应头
    // 内容类型
    // Type : applicaton/json; charset:utf-8
    // 时间 日期
    // 缓存的一些内容
    NSLog(@"%@",response.allHeaderFields);
    
    
    
    
    
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
